#!/usr/bin/env python
# -*- coding:utf-8 -*-

from flask_restful import Resource, reqparse
from flask import jsonify,g

from service.wordService import WordService
from controller.wordController import WordController
from utils import commons
from utils.response_code import RET


class WordOtherResource(Resource):

    @classmethod
    def sensitive_update(cls, word_id):
        if not word_id:
            return jsonify(code=RET.NODATA, message='primary key missed', error='primary key missed')

        parser = reqparse.RequestParser()
        parser.add_argument('is_delete', location='form', required=False, help='is_delete参数类型不正确或缺失')
        
        kwargs = parser.parse_args()
        kwargs = commons.put_remove_none(**kwargs)
        kwargs['word_id'] = word_id

        res = WordController.update(**kwargs)

        return jsonify(code=res['code'], message=res['message'], data=res['data'])

    @classmethod
    def next_word(cls,plan_id):
        user_id = g.user.get('user_id')

        word_service = WordService()
        next_word, message, plan = word_service.get_next_word(user_id, plan_id)

        if next_word:
            word_service.update_learned_word(user_id, next_word.word_id)
            remaining_target, target_unit = word_service.get_remaining_target(user_id, plan)
            learning_type = "顺序学习" if plan.plan_type == 0 else "逆序学习" if plan.plan_type == 1 else "随机学习"
            return jsonify(code=200, message=message, word={
                'word_id': next_word.word_id,
                'word_name': next_word.word_name,
                'word_paraphrase': next_word.word_paraphrase,
                'word_soundmark': next_word.word_soundmark,
                'word_pronounce': next_word.word_pronounce
            }, remaining=remaining_target, target_unit=target_unit, learning_type=learning_type)
        else:
            return jsonify(code=404, message=message)

    @classmethod
    def is_get(cls, word_id):
        if not word_id:
            return jsonify(code=RET.NODATA, message='primary key missed', error='primary key missed')

        user_id = g.user.get('user_id')
        if not user_id:
            return jsonify(code=RET.NODATA, message='user_id not found in g.user', data=None)

        word_service = WordService()
        result = word_service.is_get(user_id, word_id)

        return jsonify(code=result['code'], message=result['message'], data=result['data'])